<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Victor
 * Date: 03.12.11
 * Time: 22:17
 * To change this template use File | Settings | File Templates.
 */

class Core_Session
{


    public $login = '';
    private $sessionName = "UL";
    private $sessionId = "1";
    private $sessionCookieName = "UL";
    private $sessionIp;
    private $sessionData = array();
    private $sessionSalt = 'dasj3424sd1!d';
    private $sessionTimeToUpdate = 3600;
    private static $instance = null;

    private function Core_Session()
    {
    }

    /**
     * @static
     * @return Core_Session
     */
    public static function  getInstance()
    {
        if (self::$instance == null) {
            self::$instance = new Core_Session();
            return self::$instance;
        } else {
            if (self::$instance instanceof Core_Session) {
                return self::$instance;
            } else {
                self::$instance = new Core_Session();
                return self::$instance;
            }
        }


    }

    private function generateSessionID()
    {
        $this->sessionId = md5(crypt(md5($this->sessionSalt) . time()));

    }

    public function setSessionCookie()
    {
        //echo "set cookie sesion<br/>";
        //echo $this->sessionName . "<br/>";
        //echo $this->sessionId;
        setcookie($this->sessionName, $this->sessionId);

    }

    public function destroySession()
    {
            session_unset();
            @session_destroy();
    }

    public function createSession()
    {
        $this->destroySession();
        $this->generateSessionID();
        session_name($this->getSessionName());
        session_id($this->getSessionId());
       // var_dump($this->sessionId);
        @session_start();
        $_SESSION['sessdata'] = $this->sessionData;
        $_SESSION['my'] = true;
        $_SESSION['login'] = $this->login;
        $this->setSessionCookie('UL', session_id());

    }

    public function reopenSession($sname)
    {
        session_name($sname);
        session_start();
    }

    public function validateSession()
    {
        $valid = 0;
        if (isset($_SESSION['my'])) {
            if ($_SESSION['my'] == true) {
                $valid = 1;
            }
        }

        return $valid;
    }

    public function toString()
    {
        //var_dump($this);
    }

    public function getCookieid()
    {
        return $_COOKIE["UL"];
    }

    public
    function printInfo()
    {
        if (isset($_SESSION)) {
            //echo "<pre>SESSION</br>";
            //var_dump($_SESSION);
            //echo "COOKIE</br>";
            //var_dump($_COOKIE);
            //echo "</pre>";
        } else
        {
            //echo "Session not created.</br>";
        }


    }

    public
    function setSessionCookieName($sessionCookieName)
    {
        $this->sessionCookieName = $sessionCookieName;
    }

    public
    function getSessionCookieName()
    {
        return $this->sessionCookieName;
    }

    public
    function setSessionData($sessionData)
    {
        $this->sessionData = $sessionData;
    }

    public
    function getSessionData()
    {
        return $this->sessionData;
    }

    public
    function setSessionId($sessionId)
    {
        $this->sessionId = $sessionId;
    }

    public
    function getSessionId()
    {
        return $this->sessionId;
    }

    public
    function setSessionIp($sessionIp)
    {
        $this->sessionIp = $sessionIp;
    }

    public
    function getSessionIp()
    {
        return $this->sessionIp;
    }

    public
    function setSessionName($sessionName)
    {
        $this->sessionName = $sessionName;
    }

    public
    function getSessionName()
    {
        return $this->sessionName;
    }

    public
    function setSessionSalt($sessionSalt)
    {
        $this->sessionSalt = $sessionSalt;
    }

    public
    function getSessionSalt()
    {
        return $this->sessionSalt;
    }

    public
    function setSessionTimeToUpdate($sessionTimeToUpdate)
    {
        $this->sessionTimeToUpdate = $sessionTimeToUpdate;
    }

    public
    function getSessionTimeToUpdate()
    {
        return $this->sessionTimeToUpdate;
    }


}
